<?php

namespace app\wap\controller;



use app\common\library\WechatPay;
use app\common\service\OrderService;

/**
 * 用户
 */
class Index extends Controller {
    protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'is_login'];
    protected $noNeedRight = '*';
    protected $model = null;

    public function _initialize() {
        parent::_initialize();
    }

    /**
     *
     * 前台ajaxa获取支付链接,根据返回的url进行跳转
     *  JSAPI: 需要网页授权获取用户openid,之后两次签名加载页面,引用微信js唤起微信支付
     *  H5:直接跳转到统一下单返回的mweb_url地址即可
     * @author: justwkj
     * @date: 2021/8/6 16:31
     */
    public function get_pay_url() {
        if (!$this->auth->getUser() || !$this->auth->getUserinfo()) {
            $this->error('请先登录');
        }
        $info = $this->auth->getUserinfo();
        $user_id = $info['user_id'];
        $identify = $info['identify'];


        try {
            //智能的获取跳转链接,如果是微信内获取JSAPI链接,微信外浏览器使用H5链接
            $url = OrderService::getSmartPayUrl($identify, $user_id);
        } catch (\Exception $exception) {
            $this->error($exception->getMessage());
        }
        $this->success('ok', [
            'url' => $url
        ]);

    }

    /**
     * 微信支付唤起jsapi
     * @return \think\response\View
     * @author: justwkj
     * @date: 2021/8/26 17:47
     */
    public function pay(){
        //根据自己的业务逻辑...
        $user_id = 2;
        $identity = 1;
        $userType = 'user';

        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') === false) {
            echo '请在微信客户端内进行微信支付!';
            die;
        }
        if (!isset($_GET['code'])){ //第一次进来,需要去获取code
            //触发微信返回code码
            $baseUrl = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].'?identity='.$identity.'&user_id='.$user_id;
            $url = WechatPay::buildGetCodeUrl($baseUrl);
            header("Location: $url");
            exit();
        } else {
            //微信支付成功之后手动点击按钮跳转的页面
            $referer = 'http://'.$_SERVER['HTTP_HOST'].'/index.html#/'.$userType.'/infomation';
            //获取code码，以获取openid
            $code = $_GET['code'];
            $openid = WechatPay::getOpenId($code);
            $res = OrderService::getJsApi($identity,$user_id, $openid); //根据业务参数获取jsapi返回,准备组装js数据
            writeLog($code, 'wxpay');
            writeLog($referer, 'wxpay');
            writeLog($identity,$user_id, $openid, 'wxpay');
            writeLog($res, 'wxpay');
            $wechat = $res['wechat'];
            return view('pay', [
                'jsApiParameters' => json_encode([
                    "appId" => $wechat['appId'].'',     //公众号ID，由商户传入
                    "timeStamp" => $wechat['timeStamp'].'',     //公众号ID，由商户传入
                    "nonceStr" => $wechat['nonceStr'].'',     //公众号ID，由商户传入
                    "package" => $wechat['package'].'',     //  //"package":"prepay_id=u802345jgfjsdfgsdg888",
                    "signType" => 'MD5',     //公众号ID，由商户传入
                    "paySign" => $wechat['sign'].'',     //微信签名 70EA570631E4BB79628FBCA90534C63FF7FADD89
                ]),
                'referer' => $referer,
            ]);
        }
    }

}
